package com.guigu.controller;

import com.guigu.dto.HealthPlanDTO;
import com.guigu.dto.estimateDTO;
import com.guigu.entity.HealthPlan;
import com.guigu.entity.estimate;
import com.guigu.exception.SsmException;
import com.guigu.service.HealthPlanService;
import com.guigu.vo.CodeGenerator;
import com.guigu.vo.HealthPlanVO;
import com.guigu.vo.Result;
import com.guigu.vo.estimateVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/HealthPlan")
public class HealthPlanController {
    @Autowired
    private HealthPlanService hpservice;

    @PostMapping("/queryHealthPlan")
    public Result query(@RequestBody HealthPlanDTO dto){
        try{
            List<HealthPlanVO> list=this.hpservice.query(dto);
            System.err.println(dto);
            if(!list.isEmpty()){
                return new Result<List<HealthPlanVO>>("200", "true", "查询成功", list);
            }else if(dto.getTypename()!=null || dto.getName()!=null){
                return new Result<List<HealthPlanVO>>("200", "true", "没有对应的数据", list);
            }else{
                return new Result<List<HealthPlanVO>>("400", "false", "查询失败", null);
            }
        }catch (Exception e){
            e.printStackTrace();
            throw new SsmException("异常");
        }
    }

    @PostMapping("/addhp")
    public Result add(@RequestBody HealthPlan hp){
        try{

            Integer  id=this.hpservice.maxid();

            // 如果id为null，将其默认设置为0
            if (id==null) {
                id = 0;
            }
            //生成根据id编码
            CodeGenerator generator = new CodeGenerator("FA");
                String newCode = generator.generateCode(id+1);
                System.out.println(newCode);
                hp.setCodes(newCode);
            System.err.println(hp);
          int i=this.hpservice.addHP(hp);
          if(i==1){
              return new Result<>("200", "true", "添加成功", null);
          }else{
              return new Result<>("400", "false", "添加失败", null);
          }
        }catch (Exception e){
            e.printStackTrace();
            throw  new SsmException("异常");
        }
    }

    @PostMapping("/updatehp")
    public Result update(@RequestBody HealthPlan hp){
        try{
            int id=this.hpservice.updatehp(hp);
            if(id==1){
                return new Result<>("200", "true", "修改成功", null);
            }else{
                return new Result<>("400", "false", "修改失败", null);
            }
        }catch (Exception e){
            e.printStackTrace();
            throw  new SsmException("异常");
        }
    }

    @PostMapping("/deletehp/{id}")
    public Result delete(@PathVariable int id){
        try{
           int i= this.hpservice.deletehp(id);
           if(i==1){
               return new Result<>("200", "true", "删除成功", null);
           }else {
               return new Result<>("400", "false", "删除失败", null);
           }
        }catch (Exception e){
            e.printStackTrace();
            throw  new SsmException("异常");
        }
    }

}
